<template>
  <div id="shrz">
<!--   <h1>商户入驻</h1>-->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px " class="demo-ruleForm">
      <h3 style="text-align: left;padding-left: 5%; " >商户注册</h3><br>

      <!-- 图片上传 -->
      <el-form-item label="上传头像" prop="img" style="width: 50%;">
        <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="真实姓名" prop="name" style="width: 50%;">
        <el-input v-model="ruleForm.name" ></el-input>
      </el-form-item>

      <el-form-item label="身份证" prop="idCard" style="width: 50%;">
        <el-input v-model="ruleForm.idCard"></el-input>
      </el-form-item>

      <el-form-item label="手机号" prop="phone" style="width: 50%;">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>


      <el-form-item label="门店名称" prop="shapName" style="width: 50%;">
        <el-input v-model="ruleForm.shapName"></el-input>
      </el-form-item>

      <!-- 选择框 -->
<!--      <el-form label-width="80px"  >-->
        <el-form-item label="商户地址" prop="address" style="width: 50%;" >
          <el-input type="textarea" v-model="ruleForm.address" ></el-input>
        </el-form-item>
<!--      </el-form>-->


      <el-form-item label="详细地址" prop="detailAddress" style="width: 50%;">
        <el-input v-model="ruleForm.detailAddress"></el-input>
      </el-form-item>
      <el-form-item label="申请理由" prop="reason" style="width: 50%;">
        <el-input type="textarea" v-model="ruleForm.reason" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
   </div>
</template>

<script>
//省市区
import { pcaTextArr } from "element-china-area-data";
export default {
  name: 'shrz',
  components: {
  },
  data() {
    return {

      pcaTextArr, //地址数据
      selectedOptions: [], //地址绑定,必须是数组
      ary: '',
      //表单对象
      ruleForm: {},

      rules: {
        reason: [
          { required: true, message: '请输入理由', trigger: 'blur' }
        ],
        idCard: [
          {required: true,message: '请输入身份证号',trigger: 'blur'
          },
          {pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: '身份证号格式不正确',
            trigger: 'blur'
          }
        ],
        shapName: [
          { required: true, message: '请输入门店名称', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '请上传头像', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入商户地址', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入商户名称', trigger: 'blur' }
        ],
        detailAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ],
        selector1: [
          { required: true, message: '请选择商户地址', trigger: 'change' },
        ],

      },

    }
  },
  methods: {

    // 图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // // 密码一致
    // validateConfirmPassword(rule, value, callback) {
    //   if (value !== this.ruleForm.password) {
    //     callback(new Error('两次输入密码不一致'));
    //   } else {
    //     callback();
    //   }
    // },
      async submitForm() {
        //拿用户id
        this.sysUsersId=this.$store.state.user.sysUsersId
        console.log(this.sysUsersId)
        //定义用户id
        let sysUsersId=this.sysUsersId
        // this.resetForm.sysUsersId=sysUsersId
        console.log(this.ruleForm)
        const {data: res} = await this.$http.post(`/merchantInfo/MerchanInfoAdd/${sysUsersId}`,this.ruleForm)
        //消息提示
        this.$message({
          showClose: true,
          message: '信息填写成功,请等待审核成功',
          type: 'success'
        });
      },

    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      const selectors = ['selector1', 'selector2', 'selector3'];
      for (const selector of selectors) {
        this.$refs[selector].resetField();
      }
    },

    //级联地区改变
    handleChangeAddress(value){
      console.log(value.join('-'));
    },
  },
  created() {
    let temp = this.ary.split('-')
    console.log('ary', temp);
    this.selectedOptions = temp
  }



}
</script>

<style>
/* 图片上传 */
.avatar-uploader .el-upload {
  border: 1px dashed black;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: black;
  width: 130px;
  height: 130px;
  line-height: 130px;
  text-align: center;
}
.avatar {
  width: 130px;
  height: 130px;
  display: block;
}
</style>
